﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommunitySystemModel.WordMode;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace CommunitySystem.BO.WordExport
{
    public class WagesApprovedBO
    {
        #region 增删改查
        public ObservableCollection<Word_WagesApproved> GetWagesApproved(string condition)
        {
            ObservableCollection<Word_WagesApproved> collection = new ObservableCollection<Word_WagesApproved>();
            StringBuilder sb = new StringBuilder("select * from Word_WagesApproved");
            if (!string.IsNullOrEmpty(condition))
            {
                sb.Append("  where  ");
                sb.Append(condition);
            }
            DataSet ds = AccessHelper.GetInstance().ExcuteDataSet(sb.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    collection.Add(AccessHelper.FillDataRowToEntity<Word_WagesApproved>(ds.Tables[0], dr));
                }
            }
            return collection;
        }

        public void SaveWagesApproved(Word_WagesApproved entity)
        {
            if (entity.ID > 0)
            {
                UpdateWagesApproved(entity);
                return;
            }
            List<SqlParameter> list = null;
            string sql = AccessHelper.GeneratorInsertSQL(entity, null, "", "", out list);
            AccessHelper.GetInstance().ExcuteDataSet(sql, list.ToArray());
            
        }

        private void UpdateWagesApproved(Word_WagesApproved entity)
        {
            List<SqlParameter> list = null;
            string sql = AccessHelper.GeneratorUpdateSQL(entity, null, "", "", out list);
            AccessHelper.GetInstance().ExcuteDataSet(sql, list.ToArray());
        }

        public void DeleteWagesApproved(int id)
        {
            AccessHelper.GetInstance().DeleteEntity("Word_WagesApproved", id);
        }

        public int GetTopNOAcoordingToYear(int Year)
        {
            StringBuilder sb = new StringBuilder("select top 1 NO from Word_WagesApproved where [Year]=");
            sb.Append(Year);
            sb.Append(" order by [NO] desc ");
            DataSet ds = AccessHelper.GetInstance().ExcuteDataSet(sb.ToString());
            if (ds.Tables[0].Rows.Count <= 0)
                return 0;
            else
                return (int)ds.Tables[0].Rows[0][0];
        }
        #endregion

        #region 导出Word操作       
        public string ExportExcel(string Header, string Name, string OrginalDepartment, string OrginalPosition, string Reason, string DestDepartment, DateTime EffectiveDate, string AssignDepartment, DateTime CreateDate)
        {
            WordExportBO utility = new WordExportBO();
            WordExportBO.KillAllWordProcess();
            var _word = new Microsoft.Office.Interop.Word.Application();
            _word.Visible = true;//设置Word应用程序为不可见
            var miss = Type.Missing;
            //string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
            //string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
            //object path = uploadFolder + @"\" + @"WagesApprove.doc";// @"行政介绍信模版.doc";
            //object path = @"D:\WagesApprove.doc";// @"行政介绍信模版.doc";           
            string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
            object path = uploadFolder + @"\" + @"WagesApprove.doc";// @"行政介绍信模版.doc";
            var doc = _word.Documents.Open(ref path, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
                ref miss, ref miss);

            utility.SetPageHeader(_word, Header);
            utility.MoveLine(_word, 4);
            utility.MoveRight(_word, 3);
            int lengh = Name.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(Name);
            utility.MoveRight(_word, 15 - (lengh * 2));
            lengh = OrginalDepartment.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(OrginalDepartment);
            utility.MoveRight(_word, 30 - (lengh * 2));
            lengh = OrginalPosition.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(OrginalPosition);
            utility.MoveRight(_word, 29 - (lengh * 2));///职务
            lengh = Reason.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(Reason);//原因
            utility.MoveRight(_word, 31 - (lengh * 2));

            lengh = DestDepartment.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(DestDepartment);
            utility.MoveRight(_word, 49 - (lengh * 2));

            _word.Selection.Delete(Type.Missing, 2);
            _word.Selection.InsertAfter(EffectiveDate.Month + "");
            utility.MoveRight(_word, 4);///职务
            _word.Selection.Delete(Type.Missing, 2);
            _word.Selection.InsertAfter(EffectiveDate.Day + "");
            utility.MoveLine(_word, 5);
            _word.Selection.InsertAfter(AssignDepartment);
            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(string.Format("{0}年 {1} 月 {2}日", CreateDate.Year, CreateDate.Month, CreateDate.Day));


            utility.MoveRight(_word, 3);
            utility.MoveLine(_word, 4);
            utility.MoveRight(_word, 3);
            lengh = Name.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(Name);
            utility.MoveRight(_word, 15 - (lengh * 2));
            lengh = OrginalDepartment.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(OrginalDepartment);
            utility.MoveRight(_word, 30 - (lengh * 2));
            lengh = OrginalPosition.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(OrginalPosition);
            utility.MoveRight(_word, 29 - (lengh * 2));///职务
            lengh = Reason.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(Reason);//原因
            utility.MoveRight(_word, 31 - (lengh * 2));

            lengh = DestDepartment.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(DestDepartment);
            utility.MoveRight(_word, 49 - (lengh * 2));

            _word.Selection.Delete(Type.Missing, 2);
            _word.Selection.InsertAfter(EffectiveDate.Month + "");
            utility.MoveRight(_word, 4);///职务
            _word.Selection.Delete(Type.Missing, 2);
            _word.Selection.InsertAfter(EffectiveDate.Day + "");
            utility.MoveLine(_word, 5);
            _word.Selection.InsertAfter(AssignDepartment);
            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(string.Format("{0}年 {1} 月 {2}日", CreateDate.Year, CreateDate.Month, CreateDate.Day));


            string saveFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/cash");
            object savepath = saveFolder + @"\" + @"工资核准通知单.doc";// @"行政介绍信模版.doc";
            if (File.Exists(savepath.ToString()))
                File.Delete(savepath.ToString());
            doc.SaveAs(ref savepath, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);

            doc.Close(miss, miss, miss);

            _word.Quit();
            WordExportBO.KillAllWordProcess();
            return "/cash/工资核准通知单.doc";

        }


        public string ExportExcel(string Header, string Name, string OrginalDepartment, string OrginalPosition, string Reason, DateTime EffectiveDate, string AssignDepartment, DateTime CreateDate)
        {
            WordExportBO utility = new WordExportBO();
            WordExportBO.KillAllWordProcess();
            var _word = new Microsoft.Office.Interop.Word.Application();
            _word.Visible = true;//设置Word应用程序为不可见
            var miss = Type.Missing;
            //string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
            //object path = @"D:\WagesApprove2.doc";// @"行政介绍信模版.doc"; 
            string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/wordTemplete");
            object path = uploadFolder + @"\" + @"WagesApprove2.doc";// @"行政介绍信模版.doc";
            var doc = _word.Documents.Open(ref path, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
                ref miss, ref miss);

            utility.SetPageHeader(_word, Header);
            utility.MoveLine(_word, 4);
            utility.MoveRight(_word, 3);
            int lengh = Name.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(Name);
            utility.MoveRight(_word, 15 - (lengh * 2));
            lengh = OrginalDepartment.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(OrginalDepartment);
            utility.MoveRight(_word, 30 - (lengh * 2));
            lengh = OrginalPosition.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(OrginalPosition);
            utility.MoveRight(_word, 29 - (lengh * 2));///职务
            lengh = Reason.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(Reason);//原因
            utility.MoveRight(_word, 31 - (lengh * 2));

            //lengh = DestDepartment.Length;
            //_word.Selection.Delete(Type.Missing, lengh * 2);
            //_word.Selection.InsertAfter(DestDepartment);
            //utility.MoveRight(_word, 49 - (lengh * 2));

            _word.Selection.Delete(Type.Missing, 2);
            _word.Selection.InsertAfter(EffectiveDate.Month + "");
            utility.MoveRight(_word, 4);///职务
            _word.Selection.Delete(Type.Missing, 2);
            _word.Selection.InsertAfter(EffectiveDate.Day + "");
            utility.MoveLine(_word, 5);
            _word.Selection.InsertAfter(AssignDepartment);
            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(string.Format("{0}年 {1} 月 {2}日", CreateDate.Year, CreateDate.Month, CreateDate.Day));


            utility.MoveRight(_word, 3);
            utility.MoveLine(_word, 4);
            utility.MoveRight(_word, 3);
            lengh = Name.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(Name);
            utility.MoveRight(_word, 15 - (lengh * 2));
            lengh = OrginalDepartment.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(OrginalDepartment);
            utility.MoveRight(_word, 30 - (lengh * 2));
            lengh = OrginalPosition.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(OrginalPosition);
            utility.MoveRight(_word, 29 - (lengh * 2));///职务
            lengh = Reason.Length;
            _word.Selection.Delete(Type.Missing, lengh * 2);
            _word.Selection.InsertAfter(Reason);//原因
            utility.MoveRight(_word, 31 - (lengh * 2));

            //lengh = DestDepartment.Length;
            //_word.Selection.Delete(Type.Missing, lengh * 2);
            //_word.Selection.InsertAfter(DestDepartment);
            //utility.MoveRight(_word, 49 - (lengh * 2));

            _word.Selection.Delete(Type.Missing, 2);
            _word.Selection.InsertAfter(EffectiveDate.Month + "");
            utility.MoveRight(_word, 4);///职务
            _word.Selection.Delete(Type.Missing, 2);
            _word.Selection.InsertAfter(EffectiveDate.Day + "");
            utility.MoveLine(_word, 5);
            _word.Selection.InsertAfter(AssignDepartment);
            utility.MoveLine(_word, 2);
            _word.Selection.InsertAfter(string.Format("{0}年 {1} 月 {2}日", CreateDate.Year, CreateDate.Month, CreateDate.Day));


            string saveFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/cash");
            object savepath = saveFolder + @"\" + @"工资核准通知单.doc";// @"行政介绍信模版.doc";
            if (File.Exists(savepath.ToString()))
                File.Delete(savepath.ToString());
            doc.SaveAs(ref savepath, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);

            doc.Close(miss, miss, miss);

            _word.Quit();
            WordExportBO.KillAllWordProcess();
            return "/cash/工资核准通知单.doc";

        }
        #endregion
    }
}
